﻿using System;
using System.Collections.Generic;
using System.Text;
using MSSQLProvider.DataBase;
using System.Data;
using ConnectLib.DAO;
using System.Collections;

namespace ConnectLib.DAL
{
    public class NewsManagerBTDAL : MSSQLDataAccess
    {
        static private NewsManagerBTDAL _instance = null;

        static public NewsManagerBTDAL Instance
        {
            get
            {
                if (_instance == null)
                    _instance = (NewsManagerBTDAL)Activator.CreateInstance(typeof(NewsManagerBTDAL));
                return _instance;
            }
        }

         public NewsManagerBTDAL()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        //////////////////////////////////////////////////////////////////////////
        // ADD 
        //////////////////////////////////////////////////////////////////////////

         // Add News
         public int AddNews(NewsDAO news)
         {
             try
             {
                 List<string> arrFieldName = new List<string>();
                 List<string> arrParamName = new List<string>();
                 List<object> arrValue = new List<object>();

                 arrFieldName.Add("MaBB");
                 arrParamName.Add("@MaBB");
                 arrValue.Add(news.MaBB);

                 arrFieldName.Add("MaKB");
                 arrParamName.Add("@MaKB");
                 arrValue.Add(news.MaKB);

                 arrFieldName.Add("MaCM");
                 arrParamName.Add("@MaCM");
                 arrValue.Add(news.MaCM);

                 arrFieldName.Add("MaCMP");
                 arrParamName.Add("@MaCMP");
                 arrValue.Add(news.MaCMP);

                 arrFieldName.Add("TieuDeChinh");
                 arrParamName.Add("@TieuDeChinh");
                 arrValue.Add(news.TieuDeChinh);

                 arrFieldName.Add("TieuDePhu");
                 arrParamName.Add("@TieuDePhu");
                 arrValue.Add(news.TieuDePhu);

                 arrFieldName.Add("TomTat");
                 arrParamName.Add("@TomTat");
                 arrValue.Add(news.Tomtat);


                 arrFieldName.Add("NoiDung");
                 arrParamName.Add("@NoiDung");
                 arrValue.Add(news.Noidung);

                 arrFieldName.Add("NgayViet");
                 arrParamName.Add("@NgayViet");
                 arrValue.Add(news.NgayViet);

                 arrFieldName.Add("TrangThai");
                 arrParamName.Add("@TrangThai");
                 arrValue.Add(news.TrangThai);

                 arrFieldName.Add("MaPV");
                 arrParamName.Add("@MaPV");
                 arrValue.Add(news.MaPV);

                 arrFieldName.Add("Nguon");
                 arrParamName.Add("@Nguon");
                 arrValue.Add(news.Nguon);

                 arrFieldName.Add("LoaiBB");
                 arrParamName.Add("@LoaiBB");
                 arrValue.Add(news.LoaiBB);

                 arrFieldName.Add("Tacgia");
                 arrParamName.Add("@Tacgia");
                 arrValue.Add(news.Tacgia);

                 arrFieldName.Add("AnhDaiDien");
                 arrParamName.Add("@AnhDaiDien");
                 arrValue.Add(news.AnhDaiDien);

                 StringBuilder queryBuilder = new StringBuilder();
                 queryBuilder.Append("insert into BaiBao_BT (");
                 for (int i = 0; i < arrFieldName.Count; i++)
                 {
                     queryBuilder.Append(arrFieldName[i]);
                     if (i < arrFieldName.Count - 1)
                         queryBuilder.Append(",");
                 }

                 queryBuilder.Append(")");
                 queryBuilder.Append(" values (");
                 for (int i = 0; i < arrParamName.Count; i++)
                 {
                     queryBuilder.Append(arrParamName[i]);
                     if (i < arrParamName.Count - 1)
                         queryBuilder.Append(",");
                 }

                 queryBuilder.Append(")");

                 return ExecuteNonQuery(queryBuilder.ToString(), arrParamName, arrValue);
             }
             catch (Exception ex)
             {
                 throw ex;
             }

         }
        
        // Update News
         public int UpdateNews(NewsDAO news)
         {
             try
             {
                 string query = "update BaiBao_BT set " +
                    "MaBT=@MaBT," +
                    "MaND=@MaND," +
                    "MaKB=@MaKB," +
                    "MaCM=@MaCM," +
                    "MaCMP=@MaCMP," +
                    "TieuDeChinh=@TieuDeChinh," +
                    "TieuDePhu=@TieuDePhu," +
                    "TomTat=@TomTat," +
                    "NoiDung=@NoiDung," +
                    "NgayViet=@NgayViet," +
                    "TrangThai=@TrangThai," +
                    "LoaiBB=@LoaiBB," +
                    "Tacgia=@Tacgia," +
                    "Nguon=@Nguon," +
                    "AnhDaiDien= @AnhDaiDien," +
                    "DNNhuanBut=@DNNhuanBut," +
                    "NhuanBut=@NhuanBut " +
                    "WHERE MaBB=@MaBB";
                 
                 List<string> arrParamName = new List<string>();
                 List<object> arrValue = new List<object>();

                 arrParamName.Add("@MaBB");
                 arrValue.Add(news.MaBB);

                 arrParamName.Add("@MaBT");
                 arrValue.Add(news.MaBT);

                 arrParamName.Add("@MaND");
                 arrValue.Add(news.MaND);

                 arrParamName.Add("@MaKB");
                 arrValue.Add(news.MaKB);

                 arrParamName.Add("@MaCM");
                 arrValue.Add(news.MaCM);

                 arrParamName.Add("@MaCMP");
                 arrValue.Add(news.MaCMP);

                 arrParamName.Add("@TieuDeChinh");
                 arrValue.Add(news.TieuDeChinh);

                 arrParamName.Add("@TieuDePhu");
                 arrValue.Add(news.TieuDePhu);

                 arrParamName.Add("@TomTat");
                 arrValue.Add(news.Tomtat);

                 arrParamName.Add("@NoiDung");
                 arrValue.Add(news.Noidung);
                
                 arrParamName.Add("@NgayViet");
                 arrValue.Add(news.NgayViet);

                 arrParamName.Add("@TrangThai");
                 arrValue.Add(news.TrangThai);

                 arrParamName.Add("@MaPV");
                 arrValue.Add(news.MaPV);

                 arrParamName.Add("@Nguon");
                 arrValue.Add(news.Nguon);

                 arrParamName.Add("@LoaiBB");
                 arrValue.Add(news.LoaiBB);

                 arrParamName.Add("@Tacgia");
                 arrValue.Add(news.Tacgia);

                 arrParamName.Add("@AnhDaiDien");
                 arrValue.Add(news.AnhDaiDien);

                 arrParamName.Add("@DNNhuanBut");
                 arrValue.Add(news.DNNhuanBut);

                 arrParamName.Add("@NhuanBut");
                 arrValue.Add(news.NhuanBut);

                 return ExecuteNonQuery(query, arrParamName, arrValue);
                 
             }
             catch (Exception ex)
             {
                 throw ex;
             }

         }


        // Delete News - Se sua lai
         public bool DeleteNews(long lMaBB)
         {
             string strSQL = "Delete from DataImages_PV where maBB=" + lMaBB.ToString(); ;

             if (ExecuteNonQuery(strSQL))
             {
                 strSQL = "Delete from BaiBao_BT where maBB=" + lMaBB.ToString();
                 return ExecuteNonQuery(strSQL);
             }

             return false;
         }

         //////////////////////////////////////////////////////////////////////////
         //GET
         //////////////////////////////////////////////////////////////////////////


         // Lay danh sach tin moi nhat theo tung the loai, moi the loai 1 record
         public DataSet GetCurrNewsList()
         {
             string strSQL = "select top 15 * from BaiBao order by NgayDang desc";
             return ExecuteDataset(strSQL);
         }

         // Lay danh sach tin moi nhat theo the loai
         public DataSet GetCurrNewsList(string strMaCM, int nNumReturn)
         {

             StringBuilder sb = new StringBuilder();

             sb.AppendFormat("select top {0} * from BaiBao where MaCM={1} order by NgayDang desc", nNumReturn.ToString(), strMaCM);

             return ExecuteDataset(sb.ToString());

         }

         public List<NewsDAO> GetArrayNews_Top20(string strMaCMP)
         {
             try
             {
                 string strSQL = "select top 20 * from BaiBao where MaCMP=" + strMaCMP + " order by NgayDang desc";

                 List<Hashtable> arrHsObj = ExecuteArrayHastable(strSQL);

                 List<NewsDAO> arrNewsInfo = new List<NewsDAO>(arrHsObj.Count);

                 NewsDAO serverInfo;
                 for (int i = 0; i < arrHsObj.Count; i++)
                 {
                     serverInfo = new NewsDAO(arrHsObj[i]);
                     arrNewsInfo.Add(serverInfo);
                 }
                 return arrNewsInfo;
             }
             catch (Exception ex)
             {
                 throw ex;
             }
         }
         
         public NewsDAO LayThongTinBaiBao(long lMaBB)
         {
             try
             {
                 string query = "select top 1 BaiBao_BT.*, danhmuc.Name as TenCMP from BaiBao_BT left join danhmuc on BaiBao_BT.MaCMP=danhmuc.ID where MaBB=" + lMaBB.ToString();
                 Hashtable hsObj = ExecuteHashtable(query);

                 if (hsObj == null)
                     return null;

                 NewsDAO hotImgObj = new NewsDAO(hsObj);
                 //
                 return hotImgObj;
             }
             catch (Exception ex)
             {
                 throw ex;
             }
         }


         public DataSet GetNewsByCondition(string strSQLWhere)
         {
             StringBuilder sb = new StringBuilder();

             sb.AppendFormat("select BBV.MaCM, BBV.MaCMP, BBV.MaBB, BBV.TieuDeChinh, BBV.TrangThai, BBV.Nguon, BBV.NgayViet, danhmuc.Name as TenCMP from BaiBao_BT as BBV left join danhmuc on BBV.MaCMP=danhmuc.ID where {0} order by NgayViet desc", strSQLWhere);
             
             return ExecuteDataset(sb.ToString());
         }
         

         public long GetNewsID_Empty(int nMaPV)
         {
             StringBuilder sb = new StringBuilder();

             sb.AppendFormat("select MaBB from BaiBao_BT where MaPV={0} AND MaCM=0",nMaPV);

             object[] obj = GetObjects(sb.ToString());

             if (obj == null)
                 return 0;
             return long.Parse(obj[0].ToString());

         }

         public bool CapNhatTrangThai(long lMaBB, int nTrangThai)
         {
                          
             try
             {
                 string query = "update BaiBao_BT set TrangThai=" + nTrangThai.ToString() + " WHERE MaBB=" + lMaBB.ToString();
                 return ExecuteNonQuery(query);
                 
             }
             catch (Exception ex)
             {
                 throw ex;
             }
         }
    }
}
